//
//  SearchHelper.m
//  FoodCorner
//
//  Created by Summer on 16/3/9.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "SearchHelper.h"
#import "MainListModel.h"
#import <AVOSCloud.h>

@interface SearchHelper ()
@property (nonatomic,strong) NSMutableArray *searchArray;
@end

@implementation SearchHelper

static SearchHelper *instance = nil;

+ (instancetype)shareSearchHelper{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[SearchHelper alloc]init];
    });
    return instance;
}

- (void)requestdidFinishWithString:(NSString *)string success:(void (^)())success
{
    AVQuery *query = [MainListModel query];
    query.cachePolicy = kAVCachePolicyCacheElseNetwork;
    [query whereKey:@"title" containsString:string];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
           // NSMutableArray *array = [NSMutableArray array];
            if (_searchArray != nil) {
                [_searchArray removeAllObjects];
                for (MainListModel *model in objects) {
                    [self.searchArray addObject: model];
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                success();
            });
            
        });
    }];
}

- (NSMutableArray *)searchArray
{
    if (!_searchArray) {
        _searchArray = [NSMutableArray array];
    }
    return _searchArray;
}

- (NSInteger)numberOfDataInSearchArray
{
    return self.searchArray.count;
}

- (MainListModel *)modelOfIndexInSearchArray:(NSInteger)index
{
    return self.searchArray[index];
}




@end
